package com.blog.wl2023.service.admin;

import cn.hutool.core.util.IdUtil;
import com.blog.wl2023.entity.*;
import com.blog.wl2023.entity.LoadClass.TypePayload;
import com.blog.wl2023.entity.response.Result;
import com.blog.wl2023.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wule
 * @desc 对数据库内容更新的方法
 */
@Service
public class UpdateService {

	@Autowired
	ArticleRepository articleRepository;

	@Autowired
	CapriceRepository capriceRepository;

	@Autowired
	TypeRepository typeRepository;

	@Autowired
	ArticleCommentRepository articleCommentRepository;

	@Autowired
	TreasureChestsRepository treasureChestsRepository;

	@Autowired
	MessageListRepository messageListRepository;

	@Autowired
	UserRepository userRepository;

	@Autowired
	AnnouncementRepository announcementRepository;

	public Result uploadArticle(Article article) {
		/* article的uuid*/
		String articleUUid = IdUtil.randomUUID();
		//设置article的属性
		article.setUuid(articleUUid);
		article.setViews(0);
		article.setIsTop(0);
		article.setLikes(0);
		article.setCommentSize(0);
		article.setIsDelete(0);
		articleRepository.save(article);
		return Result.ok();

	}

	public Result uploadCaprice(Caprice caprice) {
		String uuid = IdUtil.randomUUID();
		caprice.setUuid(uuid);
		caprice.setIsDelete(0);
		capriceRepository.save(caprice);
		return Result.ok();
	}

	public Result updateAbout(Article article) {
		//只接受到内容,根据id修改
		articleRepository.updateAboutCount(article.getUuid(), article.getContent());
		return Result.ok();
	}

	public Result updateArticleIsTop(Article article) {
		if (article.getIsTop().equals(1)) {
			article.setIsTop(0);
		} else {
			article.setIsTop(1);
		}
		articleRepository.updateArticleIsTop(article.getUuid(), article.getIsTop());
		return Result.ok();
	}

	public Result updateArticleIsDelete(Article article) {
		if (article.getIsDelete().equals(1)) {
			article.setIsDelete(0);
		} else {
			article.setIsDelete(1);
		}
		articleRepository.updateArticleIsDelete(article.getUuid(), article.getIsDelete());
		return Result.ok();
	}

	public Result updateArticlesIsDelete(List<Article> articles) {
		for (Article article : articles) {
			if (article.getIsDelete().equals(0)) {
				articleRepository.updateArticleIsDelete(article.getUuid(), 1);
			}
		}
		return Result.ok();
	}

	public Result updateArticle(Article article) {
		articleRepository.updateArticle(article.getUuid(), article.getTitle(), article.getSummary(), article.getContent(), article.getTypeId(), article.getAuthor(),
				article.getQuoteLink(), article.getImageUrl(), article.getKeyOne());
		return Result.ok();
	}

	public Result updateCapriceIsDelete(Caprice caprice) {
		if (caprice.getIsDelete().equals(1)) {
			caprice.setIsDelete(0);
		} else {
			caprice.setIsDelete(1);
		}
		capriceRepository.updateCapriceIsDelete(caprice.getUuid(), caprice.getIsDelete());
		return Result.ok();
	}

	public Result updateCapricesIsDelete(List<Caprice> caprices) {

		for (Caprice caprice : caprices) {
			if (caprice.getIsDelete().equals(0)) {
				capriceRepository.updateCapriceIsDelete(caprice.getUuid(), 1);
			}
		}

		return Result.ok();
	}

	public Result updateCaprice(Caprice caprice) {
		//设置属性
		capriceRepository.updateCaprice(caprice.getUuid(), caprice.getCapriceContent());
		return Result.ok();
	}

	public Result updateType(Type type) {
		typeRepository.updateType(type.getUuid(), type.getTypeName(), type.getTypeInfo(), type.getKeyOne());
		return Result.ok();
	}

	public Result uploadType(Type type) {
		String uuid = IdUtil.randomUUID();
		type.setUuid(uuid);
		typeRepository.save(type);
		return Result.ok();
	}

	public Result deleteTypes(TypePayload types) {
		List<Type> typeList = types.getTypes();
		typeRepository.deleteAll(typeList);
		return Result.ok();
	}

	public Result updateCommentIsDelete(ArticleComment articleComment) {
		if (articleComment.getIsDelete().equals(1)) {
			articleComment.setIsDelete(0);
		} else {
			articleComment.setIsDelete(1);
		}
		articleCommentRepository.updateCommentIsDelete(articleComment.getUuid(), articleComment.getIsDelete());
		return Result.ok();
	}

	public Result updateCommentsIsDelete(List<ArticleComment> comments) {
		for (ArticleComment comment : comments) {
			if (comment.getIsDelete().equals(0)) {
				articleCommentRepository.updateCommentIsDelete(comment.getUuid(), 1);
			}
		}
		return Result.ok();
	}

	public Result updateTreasureChest(TreasureChests treasureChests) {
		treasureChestsRepository.updateTreasureChest(treasureChests.getUuid(), treasureChests.getUrl(), treasureChests.getUrlName(), treasureChests.getImageUrl(), treasureChests.getDescription());
		return Result.ok();
	}

	public Result deleteTreasureChests(List<TreasureChests> treasureChests) {
		treasureChestsRepository.deleteAll(treasureChests);
		return Result.ok();
	}

	public Result uploadTreasureChest(TreasureChests treasureChests) {
		String uuid = IdUtil.randomUUID();
		treasureChests.setUuid(uuid);
		treasureChests.setIsDelete(0);
		treasureChestsRepository.save(treasureChests);
		return Result.ok();
	}

	public Result updateMessageListIsDelete(MessageList messageList) {
		if (messageList.getIsDelete().equals(1)) {
			messageList.setIsDelete(0);
		} else {
			messageList.setIsDelete(1);
		}
		messageListRepository.updateMessageListIsDelete(messageList.getUuid(), messageList.getIsDelete());
		return Result.ok();
	}

	public Result deleteMessageLists(List<MessageList> messageLists) {
		messageListRepository.deleteAll(messageLists);
		return Result.ok();
	}

	public Result deleteUsers(List<User> users) {
		userRepository.deleteAll(users);
		return Result.ok();
	}

	public Result refuseTreasureChests(List<TreasureChests> treasureChests) {
		treasureChestsRepository.deleteAll(treasureChests);
		return Result.ok();
	}

	public Result passTreasureChests(List<TreasureChests> treasureChests) {
		treasureChests.forEach(t -> {
			treasureChestsRepository.passTreasureChests(t.getUuid());
		});
		return Result.ok();
	}

	public Result handoffShowAnnouncement(Announcement announcement) {
		if (announcement.getStatus().equals("1")) {
			announcement.setStatus("0");
		} else {
			announcement.setStatus("1");
		}
		announcementRepository.handoffShowAnnouncement(announcement.getUuid(), announcement.getStatus());
		return Result.ok();
	}

	public Result uploadAnnouncement(Announcement announcement) {
		String uuid = IdUtil.randomUUID();
		announcement.setUuid(uuid);
		announcementRepository.save(announcement);
		return Result.ok();
	}

	public Result updateAnnouncement(Announcement announcement) {
		announcementRepository.save(announcement);
		return Result.ok();
	}
}
